//
//  UIFont+CycleChangeSystemFontSize.m
//  CycleControlFunction
//
//  Created by 苹果笔记本 on 2020/5/13.
//  Copyright © 2020 cycle. All rights reserved.
//



#import "UIFont+CycleChangeSystemFontSize.h"
#import <objc/runtime.h>
#import "AppDelegate.h"
//#define kFontAppDelegate  (AppDelegate *)[UIApplication sharedApplication].delegate

///字体的category
@implementation UIFont (CycleChangeSystemFontSize)
//只执行一次的方法，在这个地方 替换方法
+ (void)initialize
{
    
}
+(void)load{
    
    //保证线程安全
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        //1.拿到系统方法 systemFontOfSize
        Method orignalMethod = class_getClassMethod(class, @selector(systemFontOfSize:));
        //1.1 拿到自己定义的方法
        Method myMethod = class_getClassMethod(class, @selector(cycle_systemFontOfSize:));
        //交换方法
        method_exchangeImplementations(orignalMethod, myMethod);
        
        
        
        //2.1 拿到系统方法 boldSystemFontOfSize
        Method orignalBoldMethod = class_getClassMethod(class, @selector(boldSystemFontOfSize:));
        //2.2 拿到自己定义的方法
        Method myBoldMethod = class_getClassMethod(class, @selector(cycle_boldSystemFontOfSize:));
        
        //交换方法
        method_exchangeImplementations(orignalBoldMethod, myBoldMethod);
        
        
        
        //3.FontWeight
        Method originFontWeight = class_getClassMethod(class, @selector(systemFontOfSize:weight:));
        Method myFontWeight     = class_getClassMethod(class, @selector(cycle_systemFontOfSize:weight:));
        //交换方法
        method_exchangeImplementations(originFontWeight, myFontWeight);
        
        
    });
}
//1.设置文字大小fontSize
+ (UIFont *)cycle_systemFontOfSize:(CGFloat)fontSize{
    AppDelegate *deleate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UIFont *font = [UIFont cycle_systemFontOfSize:fontSize*deleate.kScale];
    return font;
}
//2.设置文字大小boldFontSize
+ (UIFont *)cycle_boldSystemFontOfSize:(CGFloat)fontSize{
    AppDelegate *deleate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UIFont *font = [UIFont cycle_boldSystemFontOfSize:fontSize*deleate.kScale];
    return font;
}
//3.设置文字大小和字重
+ (UIFont *)cycle_systemFontOfSize:(CGFloat)fontSize weight:(UIFontWeight)fontWeight {
    AppDelegate *deleate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UIFont *font = [UIFont cycle_systemFontOfSize:fontSize*deleate.kScale weight:fontWeight];
    return font;
}
/*
 1.设置字体 控制器->大，中，小，可以选择，选择后刷新
 2.用处二 ，根据机型初始化的设置字体大小
 用法在Appdelegate.h中添加
 /// 字体缩放
 @property (nonatomic , assign) CGFloat kScale;
 
 Appdelegate.m中添加一个临时储存或者自行判断iphone，具体情况具体定
 
 if (![kUserDefaults objectForKey:KUserFont]) {
     [kUserDefaults setObject:@"中" forKey:KUserFont];
     self.kScale = 1.0;
 }else{
     if ([[kUserDefaults objectForKey:KUserFont]isEqualToString:@"大"]) {
         self.kScale = 1.11;
     }else if ([[kUserDefaults objectForKey:KUserFont]isEqualToString:@"中"]){
         self.kScale = 1.0;
     }else{
         self.kScale = 0.9;
     }
 }
 */
@end
